
#include "qyFileFilter.h"
#include "qyFile.h"
#ifdef _WIN32
#include <windows.h>
#endif
//-----------------------------------------------------------------------
qyFileFilter::qyFileFilter()
{

}

//-----------------------------------------------------------------------
qyFileFilter::~qyFileFilter()
{

}

#if defined(WIN32)
//-----------------------------------------------------------------------
bool qyFileFilter::LoadFileToMemory( const qyString &strAbsoluteFilePath, qyFile &file )
{
	HANDLE rf = CreateFileA( strAbsoluteFilePath.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0 );
	if ( rf == INVALID_HANDLE_VALUE )
		return false;

	unsigned long length = GetFileSize( rf, 0 );
	char *buf = (char *)file.AllockMemChunk( length );
	buf[length] = '\0';
	unsigned long bufLength;
	ReadFile( rf, buf, length, &bufLength, NULL );
	if ( bufLength != length )
	{
		file.FreeMemChunk();
		CloseHandle( rf );
		return false;
	}
	CloseHandle( rf );
	return true;
}
#else
//-----------------------------------------------------------------------
bool qyFileFilter::LoadFileToMemory( const qyString &strAbsoluteFilePath, qyFile &file )
{
	FILE *fp = fopen( strAbsoluteFilePath.c_str(), "rb" );

	if ( fp )
	{
		// get FileSize

		fseek( fp, 0, SEEK_END );
		long length = ftell( fp );
		fseek( fp, 0, SEEK_SET );
		void *buf = file.AllockMemChunk( length );
		((char*)buf)[length] = '\0';
		fread( buf, 1, length, fp);
		fclose( fp );
		return true;
	}
	return false;
}
#endif //WIN32

